package cn.lingyangwl.agile.pay.common.service.impl;

import cn.lingyangwl.framework.core.page.PageModel;
import cn.lingyangwl.framework.core.page.TableSupport;
import cn.lingyangwl.framework.data.mate.annotations.FieldBindHandle;
import cn.lingyangwl.framework.mybatis.plus.utils.PageUtil;
import cn.lingyangwl.framework.tool.core.StringUtils;
import cn.hutool.core.date.DateTime;
import cn.lingyangwl.agile.pay.common.mapper.MchNotifyRecordMapper;
import cn.lingyangwl.agile.pay.model.entity.MchNotifyRecord;
import cn.lingyangwl.agile.pay.model.rqrs.MchNotifyQL;
import cn.lingyangwl.agile.pay.common.service.MchNotifyRecordService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

/**
 * @author shenguangyang
 */
@Service
public class MchNotifyRecordServiceImpl extends ServiceImpl<MchNotifyRecordMapper, MchNotifyRecord>
        implements MchNotifyRecordService {

    /**
     * 根据订单号和类型查询
     */
    public MchNotifyRecord getByOrderAndType(String orderNo, String orderType){
        return getOne(
                MchNotifyRecord.lqw().eq(MchNotifyRecord::getOrderNo, orderNo).eq(MchNotifyRecord::getOrderType, orderType)
        );
    }

    @Override
    public MchNotifyRecord getByPayOrder(String orderNo) {
        return getByOrderAndType(orderNo, MchNotifyRecord.TYPE_PAY_ORDER);
    }

    @Override
    public MchNotifyRecord getByRefundOrder(String orderNo) {
        return getByOrderAndType(orderNo, MchNotifyRecord.TYPE_REFUND_ORDER);
    }

    @Override
    public MchNotifyRecord getByTransferOrder(String transferNo) {
        return getByOrderAndType(transferNo, MchNotifyRecord.TYPE_TRANSFER_ORDER);
    }

    @Override
    public boolean updateNotifyResult(Long notifyId, String status, String resResult) {
        return baseMapper.updateNotifyResult(notifyId, status, resResult) > 0;
    }

    @Override
    @FieldBindHandle
    public IPage<MchNotifyRecord> pageMchNotify(MchNotifyQL req) {
        PageModel pageModel = TableSupport.getPageModel();
        LocalDateTime beginTime = Optional.ofNullable(pageModel.getBeginDate()).map(DateTime::toLocalDateTime).orElse(null);
        LocalDateTime endTime = Optional.ofNullable(pageModel.getEndDate()).map(DateTime::toLocalDateTime).orElse(null);
        LambdaQueryWrapper<MchNotifyRecord> lqw = MchNotifyRecord.lqw()
                .eq(StringUtils.isNotEmpty(req.getOrderNo()), MchNotifyRecord::getOrderNo, req.getOrderNo())
                .eq(StringUtils.isNotEmpty(req.getOrderType()), MchNotifyRecord::getOrderType, req.getOrderNo())
                .eq(StringUtils.isNotEmpty(req.getMchOrderNo()), MchNotifyRecord::getMchOrderNo, req.getMchOrderNo())
                .eq(StringUtils.isNotEmpty(req.getStatus()), MchNotifyRecord::getStatus, req.getStatus())
                .eq(StringUtils.isNotEmpty(req.getAppId()), MchNotifyRecord::getAppId, req.getAppId())
                .ge(Objects.nonNull(beginTime), MchNotifyRecord::getUpdatedAt, beginTime)
                .le(Objects.nonNull(endTime), MchNotifyRecord::getUpdatedAt, endTime)
                .orderByDesc(MchNotifyRecord::getUpdatedAt);
        return this.page(PageUtil.to(pageModel), lqw);
    }

    @Override
    public boolean isExistMchNotifyRecord(String orderNo, String orderType) {
        LambdaQueryWrapper<MchNotifyRecord> lqw = MchNotifyRecord.lqw()
                .eq(MchNotifyRecord::getOrderNo, orderNo)
                .eq(MchNotifyRecord::getOrderType, orderType);
        return this.count(lqw) > 0;
    }
}
